package com.eportal.service.impl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.Serializable;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eportal.base.BaseDAO;
import com.eportal.domain.FeeAlipayAuth;
import com.eportal.domain.FuncAgent;
import com.eportal.domain.FuncApply;
import com.eportal.domain.FuncDetail;
import com.eportal.domain.FuncMain;
import com.eportal.domain.TAgent;
import com.eportal.exception.MdKnownRunTimeException;
import com.eportal.service.FuncApplyService;
import com.eportal.service.FuncMainService;
import com.eportal.service.TAgentService;

@Service("funcApplyService")
public class FuncApplyServiceImpl  implements FuncApplyService{

	@Resource(name="dao")
	private BaseDAO dao;
	@Autowired
	private TAgentService agentService;
	
	@Override
	public List<Map<String, Object>> list4audit(String funcAgentId) {
		
		FuncAgent funcAgent = (FuncAgent)dao.getById(FuncAgent.class, Long.valueOf(funcAgentId));
		String sql="select a.sid, b.communityname, c.login_account from Func_Apply a left join t03_community b on a.communityid=b.sid left join fee_alipay_auth c on a.alipay_id=c.sid where a.agtcode='"+funcAgent.getAgtcode()+"' and a.main_Id='"+funcAgent.getMainId()+"' and a.status='1'";//只取待审核的
		List<Map> list = dao.queryBySql(sql);
		TAgent agent = agentService.get(funcAgent.getAgtcode());
		FuncMain main = (FuncMain)dao.getById(FuncMain.class,Long.valueOf( funcAgent.getMainId()));
		List datalist = new ArrayList();
		for(Map bean:list){
			Map map = new HashMap();
			map.put("agtname", agent.getAgtName());
			map.put("mainName",main.getCname());
			map.put("communityname",bean.get("communityname"));
			map.put("login_account",bean.get("login_account"));
			map.put("sid",bean.get("sid"));
			datalist.add(map);
		}
		return datalist;
	}
	
	

	@Override
	public void auditYes(String[] applyArr, Long auditUser) throws Exception {
		for(String applyid:applyArr){
			if(applyid!=null&&applyid.length()>0){
				FuncApply apply = get(Long.valueOf(applyid));
				
				if("1".equals(apply.getStatus())){//只有审核中的
					apply.setAuditTime(new Date());
					apply.setAuditUsername(String.valueOf(auditUser));
					String hql="from FuncDetail where communityid='"+apply.getCommunityid()+"' and mainId='"+apply.getMainId()+"'";
					List list = dao.query(hql);
					if(list.size()>0){
						throw new MdKnownRunTimeException("id="+apply.getCommunityid()+",社区已经开通功能，请重新确认信息");
					}else{
						FeeAlipayAuth ali = (FeeAlipayAuth)dao.getById(FeeAlipayAuth.class, Long.valueOf(apply.getAlipayId()));
						Date now = new Date();
						FuncDetail bean = new FuncDetail();
						bean.setAgtcode(apply.getAgtcode());
						bean.setAuditTime(now);
						bean.setCtime(now);
						bean.setAuditUsername(String.valueOf(auditUser));
						bean.setCusername(String.valueOf(auditUser));
						bean.setStatus("1");
						bean.setAlipayId(apply.getAlipayId());
						bean.setAlipayPid(ali.getPid());
						bean.setCommunityid(apply.getCommunityid());
						bean.setMainId(apply.getMainId());
						dao.save(bean);
						apply.setStatus("2");
						apply.setAuditTime(now);
						apply.setAuditUsername(String.valueOf(auditUser));
						dao.update(apply);
					}
					
				}else{
					throw new MdKnownRunTimeException("id="+applyid+",申请状态不符合，请重新确认信息");
				}
			}
		}
		
	}



	@Override
	public FuncApply get(Long sid) {
		
		return  (FuncApply)dao.getById(FuncApply.class, sid);
	}



	@Override
	public void auditNo(String[] applyArr, Long auditUser, String comment) {
		for(String applyid:applyArr){
			if(applyid!=null&&applyid.length()>0){
				FuncApply apply = get(Long.valueOf(applyid));
				if("1".equals(apply.getStatus())){//只有审核中的
					apply.setAuditTime(new Date());
					apply.setAuditUsername(String.valueOf(auditUser));
					Date now = new Date();
					apply.setStatus("3");
					apply.setAuditTime(now);
					apply.setAuditUsername(String.valueOf(auditUser));
					apply.setAuditComment(comment);
					dao.update(apply);
				}else{
					throw new MdKnownRunTimeException("id="+applyid+",申请状态不符合，请重新确认信息");
				}
			}
		}
		
	}
	
}
